home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / bytesc88.arc / CC32.C < prev    next >
C/C++ Source or Header  |  1987-10-04  |  5KB  |  233 lines

  1. /*
  2. ** Small C - 8088/8086 version - modified by R. Grehan, BYTE Magazine
  3. */
  4.  
  5. hier13(lval)  int lval[];  {
  6.   int k;
  7.   char *ptr;
  8.   if(match("++")) {            /* ++lval */
  9.     if(hier13(lval)==0) {
  10.       needlval();
  11.       return 0;
  12.       }
  13.     step(inc, lval);
  14.     return 0;
  15.     }
  16.   else if(match("--")) {        /* --lval */
  17.     if(hier13(lval)==0) {
  18.       needlval();
  19.       return 0;
  20.       }
  21.     step(dec, lval);
  22.     return 0;
  23.     }
  24.   else if (match("~")) {        /* ~ */
  25.     if(hier13(lval)) rvalue(lval);
  26.     com();
  27.     lval[4] = ~lval[4];
  28.     return (lval[7]=0);
  29.     }
  30.   else if (match("!")) {        /* ! */
  31.     if(hier13(lval)) rvalue(lval);
  32.     lneg();
  33.     lval[4] = !lval[4];
  34.     return (lval[7]=0);
  35.     }
  36.   else if (match("-")) {        /* unary - */
  37.     if(hier13(lval)) rvalue(lval);
  38.     neg();
  39.     lval[4] = -lval[4];
  40.     return (lval[7]=0);
  41.     }
  42.   else if(match("*")) {            /* unary * */
  43.     if(hier13(lval)) rvalue(lval);
  44.     if(ptr=lval[0])lval[1]=ptr[TYPE];
  45.     else lval[1]=CINT;
  46.     lval[2]=0;        /* flag as not pointer or array */
  47.     lval[3]=0;        /* flag as not constant */
  48.     lval[4]=1;        /* omit rvalue() on func call */
  49.     lval[7]=0;
  50.     return 1;
  51.     }
  52.   else if(match("&")) {            /* unary & */
  53.     if(hier13(lval)==0) {
  54.       error("illegal address");
  55.       return 0;
  56.       }
  57.     ptr=lval[0];
  58.     lval[2]=ptr[TYPE];
  59.     if(lval[1]) return 0;
  60.                     /* global & non-array */
  61.     address(ptr);
  62.     lval[1]=ptr[TYPE];
  63.     return 0;
  64.     }
  65.   else {
  66.     k=hier14(lval);
  67.     if(match("++")) {            /* lval++ */
  68.       if(k==0) {
  69.         needlval();
  70.         return 0;
  71.         }
  72.       step(inc, lval);
  73.       dec(lval[2]>>2);
  74.       return 0;
  75.       }
  76.     else if(match("--")) {        /* lval-- */
  77.       if(k==0) {
  78.         needlval();
  79.         return 0;
  80.         }
  81.       step(dec, lval);
  82.       inc(lval[2]>>2);
  83.       return 0;
  84.       }
  85.     else return k;
  86.     }
  87.   }
  88.  
  89. hier14(lval)  int *lval; {
  90.   int k, const, val, lval2[8];
  91.   char *ptr, *before, *start;
  92.   k=primary(lval);
  93.   ptr=lval[0];
  94.   blanks();
  95.   if((cch=='[')|(cch=='(')) {
  96.     lval[5]=1;            /* secondary register will be used */
  97.     while(1) {
  98.       if(match("[")) {            /* [subscript] */
  99.         if(ptr==0) {
  100.           error("can't subscript");
  101.           junk();
  102.           needtoken("]");
  103.           return 0;
  104.           }
  105.         else if(ptr[IDENT]==POINTER)rvalue(lval);
  106.         else if(ptr[IDENT]!=ARRAY) {
  107.           error("can't subscript");
  108.           k=0;
  109.           }
  110.         setstage(&before, &start);
  111.         lval2[3]=0;
  112.         plnge2(0, 0, hier1, lval2, lval2); /* lval2 deadend */
  113.         needtoken("]");
  114.         if(lval2[3]) {
  115.           clearstage(before, 0);
  116.           if(lval2[4]) {
  117.             if(ptr[TYPE]==CINT) const2(lval2[4]<<LBPW);
  118.             else                const2(lval2[4]);
  119.             ffadd();
  120.             }
  121.           }
  122.         else {
  123.           if(ptr[TYPE]==CINT) doublereg();
  124.           ffadd();
  125.           }
  126.         lval[2]=0;
  127.         lval[1]=ptr[TYPE];
  128.         k=1;
  129.         }
  130.       else if(match("(")) {        /* function(...) */
  131.         if(ptr==0) callfunction(0);
  132.         else if(ptr[IDENT]!=FUNCTION) {
  133.           if(k && !lval[4]) rvalue(lval);
  134.           callfunction(0);
  135.           }
  136.         else callfunction(ptr);
  137.         k=lval[0]=lval[3]=lval[4]=0;
  138.         }
  139.       else return k;
  140.       }
  141.     }
  142.   if(ptr==0) return k;
  143.   if(ptr[IDENT]==FUNCTION) {
  144.     address(ptr);
  145.     lval[0]=0;
  146.     return 0;
  147.     }
  148.   return k;
  149.   }
  150.  
  151. primary(lval)  int *lval; {
  152.   char *ptr, sname[NAMESIZE];
  153.   int k;
  154.   if(match("(")) {            /* (expression,...) */
  155.     do k=hier1(lval); while(match(","));
  156.     needtoken(")");
  157.     return k;
  158.     }
  159.   putint(0, lval, 8<<LBPW);        /* clear lval array */
  160.   if(symname(sname, YES)) {
  161.     if(ptr=findloc(sname)) {
  162. #ifdef STGOTO
  163.       if(ptr[IDENT]==LABEL) {
  164.         experr();
  165.         return 0;
  166.         }
  167. #endif
  168.       getloc(ptr);
  169.       lval[0]=ptr;
  170.       lval[1]=ptr[TYPE];
  171.       if(ptr[IDENT]==POINTER) {
  172.         lval[1]=CINT;
  173.         lval[2]=ptr[TYPE];
  174.         }
  175.       if(ptr[IDENT]==ARRAY) {
  176.         lval[2]=ptr[TYPE];
  177.         return 0;
  178.         }
  179.       else return 1;
  180.       }
  181.     if(ptr=findglb(sname))
  182.       if(ptr[IDENT]!=FUNCTION) {
  183.         lval[0]=ptr;
  184.         lval[1]=0;
  185.         if(ptr[IDENT]!=ARRAY) {
  186.           if(ptr[IDENT]==POINTER) lval[2]=ptr[TYPE];
  187.           return 1;
  188.           }
  189.         address(ptr);
  190.         lval[1]=lval[2]=ptr[TYPE];
  191.         return 0;
  192.         }
  193.     ptr=addsym(sname,FUNCTION,CINT,0,&glbptr,AUTOEXT);
  194.     lval[0]=ptr;
  195.     lval[1]=0;
  196.     return 0;
  197.     }
  198.   if(constant(lval)==0) experr();
  199.   return 0;
  200.   }
  201.  
  202. experr() {
  203.   error("invalid expression");
  204.   const(0);
  205.   junk();
  206.   }
  207.  
  208. callfunction(ptr)  char *ptr; {        /* symbol table entry or 0 */
  209.   int nargs, const, val;
  210.   nargs=0;
  211.   blanks();                /* already saw open paren */
  212.   while(streq(lptr,")")==0) {
  213.     if(endst()) break;
  214.     if(ptr) {
  215.       expression(&const, &val);
  216.       push();
  217.       }
  218.     else {
  219.       push();
  220.       expression(&const, &val);
  221.       swapstk();
  222.       }
  223.     nargs=nargs+BPW;            /* count args*BPW */
  224.     if (match(",")==0) break;
  225.     }
  226.   needtoken(")");
  227.   if(streq(ptr+NAME, "CCARGC")==0) loadargc(nargs>>LBPW);
  228.   if(ptr) ffcall(ptr+NAME);
  229.   else callstk();
  230.   csp=modstk(csp+nargs, YES);
  231.   }
  232.  
  233.